﻿using UnityEngine;
using System.Collections;

namespace Room
{
    public class UIChatBubble : MonoBehaviour
    {
        public UIWidget container;
        public UILabel label;
        public UITweener tweenShow;
        public UITweener tweenHide;

        private enum State
        {
            None,
            Show,
            Hide
        }
        private const float TIME_BUBBLE_SHOW = 3f;

        private State curState = State.None;

        private float lastShowTime = 0f;

        public void Show(string text)
        {
            if (text.Length > 20)
                text = text.Insert(20, "\n");
            label.text = text;
            tweenHide.enabled = false; 
            tweenShow.ResetToBeginning();
            tweenShow.PlayForward();
            lastShowTime = Time.time;

            container.width = label.width + 28;
            container.height = label.height + 30;
            curState = State.Show;
            gameObject.SetActive(true);
        }

        void Update()
        {
            
            switch(curState)
            {
                case State.None:
                    break;
                case State.Show:
                    if (Time.time - lastShowTime > TIME_BUBBLE_SHOW)
                    {
                        tweenHide.ResetToBeginning();
                        tweenHide.PlayForward();
                        curState = State.Hide;
                    }
                    break;
                case State.Hide:
                    if (Time.time - lastShowTime > TIME_BUBBLE_SHOW + tweenHide.duration)
                    {
                        gameObject.SetActive(false);
                        curState = State.None;
                    }
                    break;
            }
        }
        
    }
}


